home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / barg / RCS / barg.c,v < prev   
Encoding:
Text File  |  1988-10-24  |  10.7 KB  |  541 lines

  1. head     1.5;
  2. access   ;
  3. symbols  ;
  4. locks    ; strict;
  5. comment  @ * @;
  6.  
  7.  
  8. 1.5
  9. date     88.10.24.14.38.02;  author nelson;  state Exp;
  10. branches ;
  11. next     1.4;
  12.  
  13. 1.4
  14. date     88.10.24.13.23.43;  author nelson;  state Exp;
  15. branches ;
  16. next     1.3;
  17.  
  18. 1.3
  19. date     88.05.17.18.16.39;  author nelson;  state Exp;
  20. branches ;
  21. next     1.2;
  22.  
  23. 1.2
  24. date     88.05.17.17.17.51;  author nelson;  state Exp;
  25. branches ;
  26. next     1.1;
  27.  
  28. 1.1
  29. date     88.05.17.16.51.54;  author nelson;  state Exp;
  30. branches ;
  31. next     ;
  32.  
  33.  
  34. desc
  35. @@
  36.  
  37.  
  38. 1.5
  39. log
  40. @Couple of minor changes.
  41. @
  42. text
  43. @/* 
  44.  * barg.c --
  45.  *
  46.  *    Bar graph generating program.  See the man page for details on
  47.  *    how to use this program.
  48.  *
  49.  * Copyright (C) 1986 Regents of the University of California
  50.  * All rights reserved.
  51.  */
  52.  
  53. #include <stdio.h>
  54.  
  55. #define    BUF_LENGTH    512
  56. char    outFile[100];
  57. int     xMinPixel = 128;
  58. int     yMinPixel = 0;
  59. int     xMaxPixel = 960;
  60. int     yMaxPixel = 384;
  61. int     numSets = -1;
  62. int     numPerSet = -1;
  63. int     xWidth = -1;
  64. float     yMaxVal;
  65. float    yValInc;
  66. float    yBaseVal;
  67. int    yBaseline;
  68.  
  69. int     yNum = -1;
  70. int     leftBorder = 48;
  71. int     rightBorder = 24;
  72. int     innerBarSpace = 8;
  73. int     innerSetSpace = -1;
  74. int     curXVal;
  75. int     pixPerSet;
  76. int     printPercent = 0;
  77. char    xLabel[100];
  78. char    yLabel[100];
  79. char    graphTitle[100];
  80. int    xLabeled = 0;
  81. int    yLabeled = 0;
  82. int    graphTitled = 0;
  83.  
  84. #define    MAX_INNER_SET_SPACE    64
  85. #define    MAX_X_WIDTH        32
  86. #define MAX_BARS        8
  87. #define INNER_SET_SPACE        32
  88. #define X_WIDTH            16
  89. #define FONT_HEIGHT        15
  90.  
  91. char    barLabels[MAX_BARS][100];
  92. int    barTypes[MAX_BARS];
  93. int    gremFilledPolys[MAX_BARS] = {1, 3, 12, 14, 16, 19, 21, 23};
  94. FILE    *inFP, *outFP;
  95. char    buf[BUF_LENGTH];
  96.  
  97. int    leftKeyBorder = 64;
  98. int    rightKeyBorder = 32;
  99. int    keyWidth = 48;
  100. int    keyHeight = 24;
  101. int    yBaseline;
  102.  
  103. main(argc, argv)
  104.     int     argc;
  105.     char **argv;
  106. {
  107.     int        i;
  108.  
  109.     if (argc <= 1) {
  110.         inFP = stdin;
  111.     } else {
  112.     inFP = fopen(argv[1], "r");
  113.     if (inFP == NULL) {
  114.         fprintf(stderr, "Couldn't open %s for reading\n", argv[1]);
  115.         exit(1);
  116.     }
  117.     if (argc <= 2) {
  118.         outFP = stdout;
  119.     } else {
  120.         outFP = fopen(argv[2], "w");
  121.         if (outFP == NULL) {
  122.         fprintf(stderr, "Couldn't open %s for writing\n", argv[2]);
  123.         exit(1);
  124.         }
  125.     }
  126.     }
  127.     fprintf(outFP, "sungremlinfile\n");
  128.     fprintf(outFP, "1 %d %d\n", xMinPixel, yMinPixel);
  129.     while (fgets(buf, BUF_LENGTH, inFP) != NULL) {
  130.     switch (buf[0]) {
  131.         case 'b':
  132.         sscanf(&buf[1], "%d %d", &leftBorder, &rightBorder);
  133.         break;
  134.         case 'd':
  135.         DrawBars();
  136.         break;
  137.         case 'h':
  138.             sscanf(&buf[1], "%d", &yMaxPixel);
  139.         break;
  140.         case 'k':
  141.         sscanf(&buf[1], "%d %d %d %d", &leftKeyBorder, &rightKeyBorder,
  142.                         &keyWidth, &keyHeight);
  143.         break;
  144.         case 'l': {
  145.         if (buf[1] == 'x') {
  146.             GetString(&buf[2], xLabel);
  147.             xLabeled = 1;
  148.         } else if (buf[1] == 'y') {
  149.             GetString(&buf[2], yLabel);
  150.             yLabeled = 1;
  151.         } else {
  152.             fprintf(stderr, "Unknown label type\n");
  153.         }
  154.         break;
  155.         }
  156.         case 'n': {
  157.         GetSetInfo();
  158.         break;
  159.         }
  160.         case 'p':
  161.         printPercent = 1;
  162.         break;
  163.         case 's':
  164.         sscanf(&buf[1], "%d %d", &innerBarSpace, &innerSetSpace);
  165.         break;
  166.         case 't':
  167.         GetString(&buf[1], graphTitle);
  168.         graphTitled = 1;
  169.         break;
  170.         case 'w':
  171.         sscanf(&buf[1], "%d", &xMaxPixel);
  172.         break;
  173.         case 'y': {
  174.         sscanf(&buf[1], "%d %f %f", &yNum, &yValInc, &yBaseVal);
  175.         yMaxVal = yNum * yValInc + yBaseVal;
  176.         if (yBaseVal < 0) {
  177.             yBaseline = (-yBaseVal / (yMaxVal - yBaseVal)) *
  178.                     (yMaxPixel - yMinPixel) + yMinPixel;
  179.         } else {
  180.             yBaseline = yMinPixel;
  181.         }
  182.         break;
  183.         }
  184.         case 'W': 
  185.         sscanf(&buf[1], "%d", &xWidth);
  186.         break;
  187.         case '1':
  188.         case '2':
  189.         case '3':
  190.         case '4':
  191.         case '5':
  192.         case '6':
  193.         case '7':
  194.         case '8': {
  195.         int    barNum;
  196.         char    *strPtr;
  197.  
  198.         barNum = buf[0] - '0' - 1;
  199.         sscanf(&buf[1], "%d", &barTypes[barNum]);
  200.         strPtr = &buf[1];
  201.         while (*strPtr == ' ') {
  202.             strPtr++;
  203.         }
  204.         while (*strPtr != ' ') {
  205.             strPtr++;
  206.         }
  207.         GetString(strPtr, barLabels[barNum]);
  208.         break;
  209.         }
  210.         case '#':
  211.         case '\n':
  212.         case ' ':
  213.         break;
  214.         default:
  215.         fprintf(stderr, "Unknown command %s\n", buf);
  216.         break;
  217.     }
  218.     }
  219.     /*
  220.      * Print the X and Y axises.
  221.      */
  222.     fprintf(outFP, "VECTOR\n");
  223.     fprintf(outFP, "%d %d\n%d %d\n", xMinPixel, yBaseline, xMaxPixel,
  224.                      yBaseline);
  225.     fprintf(outFP, "*\n6 0\n0\n");
  226.     fprintf(outFP, "VECTOR\n");
  227.     fprintf(outFP, "%d %d\n%d %d\n", xMinPixel, yMinPixel, xMinPixel, 
  228.                      yMaxPixel);
  229.     fprintf(outFP, "*\n6 0\n0\n");
  230.  
  231.     /*
  232.      * Print the dashed cross bars and labels here.
  233.      */
  234.     for (i = 0; i <= yNum; i++) {
  235.     int    yPixel;
  236.     char    label[100];
  237.  
  238.     yPixel = yMinPixel + (float)(yMaxPixel - yMinPixel) / yNum * i;
  239.     if (yPixel != yBaseline) {
  240.         fprintf(outFP, "VECTOR\n");
  241.         fprintf(outFP, "%d %d\n%d %d\n", xMinPixel, yPixel, 
  242.                          xMaxPixel, yPixel);
  243.         fprintf(outFP, "*\n1 0\n0\n");
  244.     }
  245.     if (yValInc - (int)yValInc == 0.0) {
  246.         sprintf(label, "%d", (int) (i * yValInc + yBaseVal));
  247.     } else {
  248.         sprintf(label, "%0.2f", i * yValInc + yBaseVal);
  249.     }
  250.     if (printPercent) {
  251.         strcat(label, "%");
  252.     }
  253.     PutString(label, xMinPixel - 10, yPixel, "CENTRIGHT", 2);
  254.     }
  255.  
  256.     /*
  257.      * Print the axis labels.
  258.      */
  259.     if (xLabeled) {
  260.     PutString(xLabel, xMinPixel + (xMaxPixel - xMinPixel) / 2,
  261.           yMinPixel - 48, "TOPCENT", 2);
  262.     }
  263.     if (yLabeled) {
  264.     int    yPix;
  265.     char    chBuf[2];
  266.     char    *strPtr;
  267.  
  268.     chBuf[1] = '\0';
  269.     yPix = yMaxPixel - 
  270.            (yMaxPixel - yMinPixel - strlen(yLabel) * FONT_HEIGHT) / 2;
  271.     for (strPtr = yLabel;
  272.          *strPtr != '\0';
  273.          strPtr++, yPix -= FONT_HEIGHT) {
  274.         chBuf[0] = *strPtr;
  275.         PutString(chBuf, xMinPixel - 80, yPix, "TOPCENT", 2);
  276.     }
  277.     }
  278.  
  279.     /*
  280.      * Print the graph title.
  281.      */
  282.     if (graphTitled) {
  283.     PutString(graphTitle, xMinPixel + (xMaxPixel - xMinPixel) / 2,
  284.            yMaxPixel + 64, "TOPCENT", 3);
  285.     }
  286.  
  287.     /*
  288.      * Print out bar key.
  289.      */
  290.     PrintBarKey();
  291.  
  292.     fprintf(outFP, "-1\n");
  293. }
  294.  
  295. PutString(label, x, y, loc, font)
  296.     char    *label;
  297.     int        x;
  298.     int        y;
  299.     char    *loc;
  300.     int        font;
  301. {
  302.     fprintf(outFP, "%s\n", loc);
  303.     fprintf(outFP, "%d %d\n", x, y);
  304.     fprintf(outFP, "0 0\n0 0\n0 0\n*\n1 %d\n", font);
  305.     fprintf(outFP, "%d %s\n", strlen(label), label);
  306. }
  307.  
  308. GetString(srcStr, destStr)
  309.     char    *srcStr;
  310.     char    *destStr;
  311. {
  312.     char    *strPtr;
  313.  
  314.     while (*srcStr == ' ') {
  315.     srcStr++;
  316.     }
  317.     strPtr = srcStr;
  318.     while (*strPtr != '\n') {
  319.     strPtr++;
  320.     }
  321.     *strPtr = '\0';
  322.     strcpy(destStr, srcStr);
  323. }
  324.  
  325. DrawFilledRectangle(minX, minY, width, height, fillType)
  326.     int    minX;
  327.     int    minY;
  328.     int width;
  329.     int height;
  330.     int fillType;
  331. {
  332.     fprintf(outFP, "POLYGON\n");
  333.     fprintf(outFP, "%d %d\n", minX, minY + height);
  334.     fprintf(outFP, "%d %d\n", minX + width, minY + height);
  335.     fprintf(outFP, "%d %d\n", minX + width, minY);
  336.     fprintf(outFP, "%d %d\n", minX, minY);
  337.     fprintf(outFP, "*\n");
  338.     fprintf(outFP, "5 %d\n", gremFilledPolys[fillType]);
  339.     fprintf(outFP, "0\n");
  340.  
  341. }
  342.  
  343. PrintBarKey()
  344. {
  345.     int    perKeyWidth;
  346.     int    baseX;
  347.     int    baseY;
  348.     int    i;
  349.  
  350.     perKeyWidth = (xMaxPixel - xMinPixel - leftKeyBorder - rightKeyBorder) / 
  351.                                     numPerSet;
  352.     if (perKeyWidth < keyWidth * 3) {
  353.     perKeyWidth = keyWidth * 3;
  354.     }
  355.     if (xLabeled) {
  356.     baseY = yMinPixel - keyHeight - 96;
  357.     } else {
  358.     baseY = yMinPixel - keyHeight - 64;
  359.     }
  360.     for (i = 0; i < numPerSet; i++) {
  361.     baseX = xMinPixel + leftKeyBorder + perKeyWidth * i;
  362.     DrawFilledRectangle(baseX, baseY, keyWidth, keyHeight, barTypes[i]);
  363.     PutString(barLabels[i], baseX + keyWidth + 16, baseY + keyHeight / 2,
  364.           "CENTLEFT", 2);
  365.     }
  366. }
  367.  
  368. DrawBars()
  369. {
  370.     float    val;
  371.     int        yHeight;
  372.     char    label[100];
  373.     char    *labelPtr;
  374.     int        i = 0;
  375.  
  376.     i = 1;
  377.     while (buf[i] == ' ') {
  378.     i++;
  379.     }
  380.     for (labelPtr = label; buf[i] != '\n'; i++, labelPtr++) {
  381.     *labelPtr = buf[i];
  382.     }
  383.     *labelPtr = '\0';
  384.  
  385.     PutString(label, curXVal + pixPerSet / 2, yMinPixel - 10,
  386.           "TOPCENT", 2);
  387.     i = 0;
  388.     while (fgets(buf, BUF_LENGTH, inFP) != NULL) {
  389.     if (buf[0] == 'e') {
  390.         break;
  391.     }
  392.     if (i >= numPerSet) {
  393.         fprintf(stderr, "Too many bars per set \"%s\"\n", label);
  394.         i++;
  395.         continue;
  396.     }
  397.     sscanf(buf, "%f", &val);
  398.     if (val > yMaxVal) {
  399.         fprintf(stderr, "Y Value %4.2f clipped\n", val);
  400.         val = yMaxVal;
  401.     } else if (val < yBaseVal) {
  402.         fprintf(stderr, "Y Value %4.2f clipped\n", val);
  403.         val = yBaseVal;
  404.     }
  405.     if (yBaseVal >= 0) {
  406.         yHeight = ((val - yBaseVal) / (yMaxVal - yBaseVal)) * 
  407.                             (yMaxPixel - yMinPixel);
  408.     } else if (val >= 0) {
  409.         yHeight = (val / yMaxVal) * (yMaxPixel - yBaseline);
  410.     } else {
  411.         yHeight = (val / yBaseVal) * (yBaseline - yMinPixel);
  412.     }
  413.     if (yHeight < 4) {
  414.         fprintf(outFP, "VECTOR\n");
  415.         fprintf(outFP, "%d %d\n%d %d\n", curXVal, yBaseline + 2,                    curXVal + xWidth, yBaseline + 2);
  416.         fprintf(outFP, "*\n3 0\n0\n");
  417.     } else {
  418.         if (val < 0) {
  419.         DrawFilledRectangle(curXVal, yBaseline - yHeight, xWidth,
  420.                     yHeight, barTypes[i]);
  421.         } else {
  422.         DrawFilledRectangle(curXVal, yBaseline, xWidth,
  423.                     yHeight, barTypes[i]);
  424.         }
  425.     }
  426.     i++;
  427.     curXVal += xWidth + innerBarSpace;
  428.     }
  429.     curXVal += innerSetSpace - innerBarSpace;
  430. }
  431.  
  432. GetSetInfo()
  433. {
  434.     int    availPixels;
  435.     int    width;
  436.     
  437.     sscanf(&buf[1], "%d %d", &numSets, &numPerSet);
  438.     availPixels = xMaxPixel - xMinPixel - leftBorder - rightBorder;
  439.     if (innerSetSpace == -1) {
  440.     if (xWidth == -1) {
  441.         xWidth = X_WIDTH;
  442.     }
  443.     availPixels -= xWidth * numSets * numPerSet +
  444.                innerBarSpace * numSets * (numPerSet - 1);
  445.     innerSetSpace = availPixels / (numSets - 1);
  446.     if (innerSetSpace > MAX_INNER_SET_SPACE) {
  447.         innerSetSpace = MAX_INNER_SET_SPACE;
  448.     }
  449.     } else if (xWidth == -1) {
  450.     availPixels -= (numSets - 1) * innerSetSpace;
  451.     pixPerSet = availPixels / numSets;
  452.     xWidth = (pixPerSet - (numPerSet - 1) * innerBarSpace) / 
  453.                             numPerSet;
  454.     if (xWidth > MAX_X_WIDTH) {
  455.         xWidth = MAX_X_WIDTH;
  456.     }
  457.     }
  458.     pixPerSet = xWidth * numPerSet + 
  459.         innerBarSpace * (numPerSet - 1);
  460.     width = leftBorder + rightBorder + pixPerSet * numSets + 
  461.         innerSetSpace * (numSets - 1);
  462.     xMaxPixel = xMinPixel + width;
  463.     curXVal = xMinPixel + leftBorder;
  464. }
  465. @
  466.  
  467.  
  468. 1.4
  469. log
  470. @*** empty log message ***
  471. @
  472. text
  473. @d1 10
  474. d42 6
  475. a47 5
  476. #define    MAX_X_WIDTH    32
  477. #define MAX_BARS    8
  478. #define INNER_SET_SPACE    32
  479. #define X_WIDTH        16
  480. #define FONT_HEIGHT    15
  481. d310 3
  482. d404 3
  483. @
  484.  
  485.  
  486. 1.3
  487. log
  488. @*** empty log message ***
  489. @
  490. text
  491. @d317 1
  492. d320 9
  493. a328 1
  494.     sscanf(&buf[1], "%s", label);
  495. d331 1
  496. @
  497.  
  498.  
  499. 1.2
  500. log
  501. @*** empty log message ***
  502. @
  503. text
  504. @d27 1
  505. d30 1
  506. d113 4
  507. d200 1
  508. a200 1
  509.     PutString(label, xMinPixel - 10, yPixel, "CENTRIGHT");
  510. d208 1
  511. a208 1
  512.           yMinPixel - 48, "TOPCENT");
  513. d222 1
  514. a222 1
  515.         PutString(chBuf, xMinPixel - 80, yPix, "TOPCENT");
  516. d227 8
  517. d242 1
  518. a242 1
  519. PutString(label, x, y, loc)
  520. d247 1
  521. d251 1
  522. a251 1
  523.     fprintf(outFP, "0 0\n0 0\n0 0\n*\n1 2\n");
  524. d308 1
  525. a308 1
  526.           "CENTLEFT");
  527. d321 1
  528. a321 1
  529.           "TOPCENT");
  530. @
  531.  
  532.  
  533. 1.1
  534. log
  535. @Initial revision
  536. @
  537. text
  538. @d151 4
  539. d310 5
  540. @
  541.